//
//  IWelcomePage.m
//  XRmenu
//
//  Created by yangxh on 11-3-24.
//  Copyright 2011 E0571. All rights reserved.
//

#import "IWelcomePage.h"
#import "Plist.h"

@implementation IWelcomePage

@synthesize title;
@synthesize titleTop;
@synthesize titleLeft;
@synthesize content;
@synthesize titleColor;
@synthesize contentColor;
@synthesize contentTop;
@synthesize contentLeft;
@synthesize imageFile;
@synthesize show;

//=========================================================== 
// - (id)init
//
//=========================================================== 
- (id)init
{
	return [self initWithTitle:nil titleTop:0 titleLeft:0 content:nil titleColor:0 contentColor:0 contentTop:0 contentLeft:0 imageFile:nil show:NO];
}


//=========================================================== 
// - (id)initWith:
//
//=========================================================== 
- (id)initWithTitle:(NSString*)aTitle titleTop:(NSInteger)aTitleTop titleLeft:(NSInteger)aTitleLeft content:(NSArray*)aContent titleColor:(NSInteger)aTitleColor contentColor:(NSInteger)aContentColor contentTop:(NSInteger)aContentTop contentLeft:(NSInteger)aContentLeft imageFile:(NSString*)anImageFile show:(BOOL)flag 
{
    self = [super init];
    if (self) {
        self.title = aTitle;
        self.titleTop = aTitleTop;
        self.titleLeft = aTitleLeft;
        self.content = aContent;
        self.titleColor = aTitleColor;
        self.contentColor = aContentColor;
        self.contentTop = aContentTop;
        self.contentLeft = aContentLeft;
        self.imageFile = anImageFile;
        self.show = flag;
    }
    return self;
}


//=========================================================== 
// + (id)objectWith:
//
//=========================================================== 
+ (id)objectWithTitle:(NSString*)aTitle titleTop:(NSInteger)aTitleTop titleLeft:(NSInteger)aTitleLeft content:(NSArray*)aContent titleColor:(NSInteger)aTitleColor contentColor:(NSInteger)aContentColor contentTop:(NSInteger)aContentTop contentLeft:(NSInteger)aContentLeft imageFile:(NSString*)anImageFile show:(BOOL)flag  
{
    id result = [[[self class] alloc] initWithTitle:aTitle titleTop:aTitleTop titleLeft:aTitleLeft content:aContent titleColor:aTitleColor contentColor:aContentColor contentTop:aContentTop contentLeft:aContentLeft imageFile:anImageFile show:flag];
	
    return [result autorelease];
}

//=========================================================== 
// - (NSArray *)keyPaths
//
//=========================================================== 
- (NSArray *)keyPaths
{
    NSArray *result = [NSArray arrayWithObjects:
					   @"title",
					   @"titleTop",
					   @"titleLeft",
					   @"content",
					   @"titleColor",
					   @"contentColor",
					   @"contentTop",
					   @"contentLeft",
					   @"imageFile",
					   @"show",
					   nil];
	
    return result;
}


//=========================================================== 
// - (NSString *)descriptionForKeyPaths
//
//=========================================================== 
- (NSString *)descriptionForKeyPaths 
{
    NSMutableString *desc = [NSMutableString string];
    NSEnumerator *e = [[self keyPaths] objectEnumerator];
    NSString *thisKey;
    [desc appendString:@"\n\n"];
	
    while ((thisKey = [e nextObject])) {
        [desc appendFormat: @"%@: %@\n", thisKey, [self valueForKey:thisKey]];
    }
	
    return [NSString stringWithString:desc];
}

- (NSString *)description
{
	return [self descriptionForKeyPaths];
}

//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc
{
    [title release];
    title = nil;
    [content release];
    content = nil;
    [imageFile release];
    imageFile = nil;
	
    [super dealloc];
}

+ (id)objectFromPlist:(NSString *)FileName
{
	NSDictionary *dataRoot = [Plist loadFromFile:FileName];
	[dataRoot retain];
	NSDictionary *data = [dataRoot objectForKey:@"Welcome"];
	IWelcomePage *welcome = [IWelcomePage iWelcomePageByParse:data];
	[dataRoot release];
	return welcome;
}

- (void)saveToPlist:(NSString *)FileName
{
	NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:title,@"Title",
						  num(titleTop),@"TitleTop",
						  num(titleLeft),@"TitleLeft",
						  num(titleColor),@"TitleColor",
						  num(contentTop),@"ContentTop",
						  num(contentLeft),@"ContentLeft",
						  content,@"Content",
						  num(contentColor),@"ContentColor",
						  imageFile,@"ImageFile",
						  [NSNumber numberWithBool:show],@"Show",
						  nil];
	NSDictionary *datas = [NSDictionary dictionaryWithObjectsAndKeys:data,@"Welcome",nil];
	[Plist saveToFile:FileName Data:datas];
}

+ (IWelcomePage *)iWelcomePageByParse:(NSDictionary *)info
{
	IWelcomePage *welcome = [IWelcomePage objectWithTitle:[info objectForKey:@"Title"] titleTop:dictInt(info,@"TitleTop") titleLeft:dictInt(info,@"TitleLeft") content:[info objectForKey:@"Content"] titleColor:dictInt(info,@"TitleColor") contentColor:dictInt(info,@"ContentColor") contentTop:dictInt(info,@"ContetnTop") contentLeft:dictInt(info,@"ContentLeft") imageFile:[info objectForKey:@"ImageFile"] show:NO];
	return welcome;
}

@end
